package com.supertimetable.db;

import android.content.Context;
import android.database.Cursor;

import com.supertimetable.bean.Course;
import com.supertimetable.bean.TeacherTable;
import com.supertimetable.constant.Constant;


/**
 * Created by ooyhao on 2018/1/10.
 */

public class DBTeacherTableUtil {

    //插入一堂课的信息
    public static void insertCourse(Context context, Course course, String tId, int week, int day){
        String id = tId;
//        int week = course.getWeekTime();
//        int day = course.getDayTime();
        String content = "";
        if (course != null){
            content = course.getCourseContent();
        }
        System.out.println(id+"-"+day+"-"+week+"-"+content+"insertcourse syso");
        try {
            System.out.println("insert -----------插入一堂课");
            MyDBHelper helper = new MyDBHelper(context);
            String sql = "insert into "+ Constant.TEACHERTABLE_TABLE
 +"(tId,weekTime,dayTime,courseContent) values" +
                    " ('"+id+"',"+week+","+day+",'"+content+"')";
            helper.getWritableDatabase().execSQL(sql);
            System.out.println(sql);
        }catch (Exception e){
            System.out.println("插入课程异常");
        }
    }
    //插入一张教师课表的信息
    public static void insertTeacherTable(Context context, TeacherTable tt, String tid){
        if (tt == null){
            System.out.println("插入教师课表");
            return;

        }

        Course [][] cs = tt.getCourses();
        for(int i = 0; i < cs.length;i++){
            for(int j = 0; j < cs[i].length; j++){
                insertCourse(context,cs[i][j],tid,j,i);
            }
        }
    }

    //查询一张教师课表的信息
    public static TeacherTable queryCourse(Context context,String tid){
        TeacherTable tt = new TeacherTable();
        tt.setTeacherName(tid);
        Course [][] courses = new Course[6][7];
        try {
            MyDBHelper helper = new MyDBHelper(context);
            String sql = "select * from "+Constant.TEACHERTABLE_TABLE +" where tId = '"+tid+"'";
//            String sql = "select * from "+Constant.TEACHERTABLE_TABLE;
            Cursor cursor = helper.getWritableDatabase().rawQuery(sql,null);
            System.out.println("查询课程："+sql);
            while (cursor.moveToNext()){
                Course c = new Course();
                int weekTime = cursor.getInt(cursor.getColumnIndex("weekTime"));
                int dayTime = cursor.getInt(cursor.getColumnIndex("dayTime"));
                c.setWeekTime(weekTime);
                c.setDayTime(dayTime);
                c.setCourseContent(cursor.getString(cursor.getColumnIndex("courseContent")));

                courses[dayTime][weekTime] = c;
                System.out.print("tId:"+cursor.getString(cursor.getColumnIndex("tId")));
                System.out.print("  weekTime:"+cursor.getInt(cursor.getColumnIndex("weekTime")));
                System.out.print("  dayTime:"+cursor.getInt(cursor.getColumnIndex("dayTime")));
                System.out.println("  courseContent:"+cursor.getString(cursor.getColumnIndex("courseContent")));

            }
            tt.setCourses(courses);
            System.out.println(sql);
        }catch (Exception e){
            System.out.println("查询课程异常");
        }
        return tt;
    }
    //删除教师表中的信息
    public static void delectTeacherTable(Context context){
        try {
            MyDBHelper helper = new MyDBHelper(context);
            String sql = "delete from "+ Constant.TEACHERTABLE_TABLE;
            helper.getWritableDatabase().execSQL(sql);
            System.out.println("删除");
        }catch (Exception e){
            System.out.println("删除表数据异常");
        }
    }

    //删除教师表
    public static void dropTeacherTable(Context context){
        try {
            MyDBHelper helper = new MyDBHelper(context);
            String sql = "drop table "+Constant.TEACHERTABLE_TABLE;
            helper.getWritableDatabase().execSQL(sql);

        }catch (Exception e){
            System.out.println("删除表异常");
        }
    }





//TODO 测试下载教师

//        new Thread(){
//            @Override
//            public void run() {
//                try {
////                    String path = "http://qg.peizheng.net.cn/ZNPK/Private/List_JS.aspx?xnxq=20170&js=";
////                    String cookie = "ASP.NET_SessionId=humciw554ebnzrazkwnn3i55";
////                    String referer = "http://qg.peizheng.net.cn/ZNPK/TeacherKBFB.aspx";
////                    String params = " ";
//
//
//                    sb = HTMLUtils.getHTML(path,cookie,referer,params);
//                    teachers = (ArrayList<Teacher>) AnalysisUtils.getTeachers(sb);
//                    Log.i("size",teachers.size()+"");
//
//                    ArrayList<String> str = getStringFromList(teachers);
//
//                    adapter = new ArrayAdapter<String>(TeacherTableActivity.this,android.R.layout.simple_list_item_1,str);
//
//
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(TeacherTableActivity.this,teachers.size()+"",Toast.LENGTH_SHORT).show();
//
//                            lv.setAdapter(adapter);
//                        }
//                    });
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }.start();

//        StringBuffer buffer = Html_Buffer.getStringBufferFromHTML();
//        tt = AnalysisUtils.getCourse(buffer);


//        Log.i("tt.name",tt.getTeacherTableName());
//        Log.i("tt.totalTime",tt.getTotalTime());
//        Log.i("tt-teacherName",tt.getTeacherName());
//        Log.i("tt.teacherName",tt.getTeacherName());
}
